package com.xin.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xin.mmall.entity.Cart;
import com.xin.mmall.entity.Product;
import com.xin.mmall.enums.ResultEnum;
import com.xin.mmall.execption.MMallException;
import com.xin.mmall.mapper.CartMapper;
import com.xin.mmall.mapper.ProductMapper;
import com.xin.mmall.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.mmall.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Service
@Slf4j
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public boolean save(Cart entity) {
        //减库存
        Product product = productMapper.selectById(entity.getProductId());
        Integer stock = product.getStock()-entity.getQuantity();
        if (stock < 0){
            //打印日志
            log.error("【添加购物车】库存不足！stock={}",stock);
            throw new MMallException(ResultEnum.STOCK_ERROR);
        }
        product.setStock(stock);
        //保存,库存-1且购物车记录添加，返回true，否则返回false
        if (productMapper.updateById(product)==1 && cartMapper.insert(entity)==1)
            return true;
        return false;
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        //通过cartID找到productId
        Integer id = (Integer) columnMap.get("id");
        Cart cart = cartMapper.selectById(cartMapper.selectById(id));
        Integer productId = cart.getProductId();
        //将cart记录删除
        cartMapper.deleteById(id);
        //通过productId查到对应的product
        Product product = productMapper.selectById(productId);
        System.out.println(product);
        //将product的库存增加quantity个
        System.out.println("删除前的库存为："+product.getStock());
        product.setStock(product.getStock()+(Integer) columnMap.get("quantity"));
        System.out.println("删除后的库存为："+product.getStock());
        productMapper.updateById(product);
        return true;
    }

    @Override
    public List<CartVo> findAllCartVOByUserId(Integer id) {
        List<CartVo> cartVoList = new ArrayList<>();
        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id",id);
        List<Cart> cartList = cartMapper.selectList(cartQueryWrapper);
        for (Cart cart : cartList) {
            CartVo cartVo = new CartVo();
            Product product = productMapper.selectById(cart.getProductId());
            BeanUtils.copyProperties(product,cartVo);
            BeanUtils.copyProperties(cart,cartVo);
            cartVo.setProductId(product.getId());
            cartVoList.add(cartVo);
        }
        return cartVoList;
    }
}
